<template>
	<div>
		<el-dialog :visible.sync="visible1" title="负责人信息" top="3%" width="30%">
			<el-form ref="form" :model="form" :rules="formRules" label-width="100px">

				<el-form-item label="负责人" prop="departmentManagerName">
					<el-input v-model="form.departmentManagerName" style="width: 100%;"></el-input>
				</el-form-item>

				<el-form-item label="部门名称" prop="departmentCode">
					<el-select v-model="form.departmentCode" placeholder="请选择部门" style="width: 100%;">
						<el-option :label="item.name" :value="item.code" v-for="(item,index) in departmentList">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			
			<div style="text-align: center;" slot="footer">
				<!-- <el-button v-if="form && form.id" type="danger" @click="deleteThis">删除</el-button> -->
				<el-button type="primary" @click="saveOrUpdate">保存</el-button>
				<el-button @click="cancle">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	let that
	export default {
		name: 'addDepartment',
		components: {},
		props: ['entId', 'editData'],
		props: {
			enId: {
				type: String,
			},
			editData: {
				type: [String, Object]
			},
			departmentType: {
				type: String,
				default: 'HW_DEPARTMENT'
			}
		},
		data() {
			return {
				visible1: false,
				params: {
					facilitatorId: '',
					categoryMainId: '',
					ticketId: '',
					itemId: '',
					subjectId: '',
				},
				form: {
					facilitatorId: '',
					departmentDesc: '',
					departmentCode: '',
					departmentManagerName: ''
				},
				formRules: {
					departmentManagerName: [{
						required: true,
						message: '请输入负责人',
						trigger: 'blur'
					}],
					departmentCode: [{
						required: true,
						message: '请选择部门',
						trigger: 'blur'
					}]
				},
				departmentList: [],
			}
		},
		created: function() {},
		mounted: function() {
			that = this
			that.params.facilitatorId = that.getQueryString("facilitatorId")
			that.params.categoryMainId = that.getQueryString("categoryMainId")
			that.params.ticketId = that.$store.state.token
			that.params.itemId = that.getQueryString("itemId")
			that.params.subjectId = that.getQueryString("subjectId")

			this.getDepartmentList();

			//if (that.editData) that.form = that.editData
		},
		methods: {
			open(editData) {
				if (editData) {
					that.form = editData
				} else {
					that.form = that.$options.data().form
				}
				that.visible1 = true
			},
			
			cancle() {
				that.visible1 = false
				that.$emit("cancle", true)
			},

			//删除危废
			deleteThis() {
				this.$confirm('此操作将删除此数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					that.ajax({
						url: '/facilitatorDepartment/deleteFacilitatorDepartment?ticketId=' + that.params
							.ticketId,
						data: {
							departmentId: that.form.id
						},
						contentType: 'application/x-www-form-urlencoded',
						success: function(result) {
							if (result.status && result.data) {
								that.visible1 = false
								that.$emit("success", true)
							}
						}
					})
				})
			},


			saveOrUpdate: function() {
				var _this = this;
				this.$refs['form'].validate(function(valid) {
					if (valid) {
						_this.form.facilitatorId = that.params.facilitatorId
						_this.form.departmentType = that.departmentType
						that.ajax({
							url: '/facilitatorDepartment/saveOrUpdateFacilitatorDepartment?ticketId=' +
								that.params.ticketId,
							data: JSON.stringify(_this.form),
							contentType: 'application/json',
							success: function(result) {
								if (result.data) {
									that.visible1 = false
									that.data = that.$options.data()
									that.$forceUpdate()
									if (_this.form.id) {
										that.$emit("success", true)
									} else {
										that.$emit("success", true)
									}
									that.$message.success("保存成功！")
								} else {}
							}
						});
					}
				});
			},

			getDepartmentList: function() {
				var _this = this;
				that.ajax({
					url: '/facilitatorDepartment/getFacilitatorDepartmentCodeList',
					data: {
						departmentType: that.departmentType
					},
					contentType: 'application/x-www-form-urlencoded',
					success: function(data) {
						if (data.status == 1) {
							_this.departmentList = data.data;
						}
					}
				});
			},

		}
	}
</script>

<style>

</style>